

def perm(dim, needBeauty=False):
    result = []
    xstack = []

    arr = []
    # xset = set(range(dim, 0, -1))
    xset = set(range(1, dim + 1))

    xstack.append((arr, xset))

    while len(xstack):
        theArr, theSet = xstack.pop()
        if needBeauty:
            sortedInts = sorted(list(theSet))[::-1]
        else:
            sortedInts = theSet
        for theInt in sortedInts:
            newSet = theSet.copy()
            newSet.remove(theInt)
            newArr = theArr.copy()
            newArr.append(theInt)
            if dim == len(newArr):
                result.append(newArr)
            else:
                xstack.append((newArr, newSet))
    return result


def print_perm(xperm):
    n = 1
    for line in xperm:
        print(n, '\t', line)
        n += 1


def perm_and_print(dim):
    xperm = perm(dim)
    print_perm(xperm)

for i in range(1, 5):
    print('-' * 32)
    perm_and_print(i)
